package tool

import (
	"GameGo/config"
	"github.com/hajimehoshi/ebiten/v2"
	"image/color"
)

func DrawPlayerHPBar(screen *ebiten.Image, hp, maxHP int) {
	barWidth := float64(config.ScreenXSize)
	barHeight := 20.0
	if maxHP <= 0 {
		return // 防止除0
	}
	// 比例
	rate := float64(hp) / float64(maxHP)
	if rate < 0 {
		rate = 0
	}
	if rate > 1 {
		rate = 1
	}
	width := int(barWidth * rate)
	if width <= 0 {
		width = 1 // 至少画出一个像素
	}

	bg := ebiten.NewImage(int(barWidth), int(barHeight))
	bg.Fill(color.RGBA{50, 50, 50, 255})
	opt := &ebiten.DrawImageOptions{}
	opt.GeoM.Translate(0, 0)
	screen.DrawImage(bg, opt)

	// 血量条
	fg := ebiten.NewImage(width, int(barHeight))
	fg.Fill(color.RGBA{0, 255, 0, 255})
	opt2 := &ebiten.DrawImageOptions{}
	opt2.GeoM.Translate(0, 0)
	screen.DrawImage(fg, opt2)
}

func DrawEnemyHPBar(screen *ebiten.Image, x, y float64, hp, maxHP int, barWidth, barHeight float64) {
	if maxHP <= 0 {
		return // 防止除0
	}
	// 比例
	rate := float64(hp) / float64(maxHP)
	if rate < 0 {
		rate = 0
	}
	if rate > 1 {
		rate = 1
	}
	width := int(barWidth * rate)
	if width <= 0 {
		width = 1 // 至少画出一个像素
	}

	bg := ebiten.NewImage(int(barWidth), int(barHeight))
	bg.Fill(color.RGBA{50, 50, 50, 255})
	opt := &ebiten.DrawImageOptions{}
	opt.GeoM.Translate(x, y)
	screen.DrawImage(bg, opt)

	// 血量条
	fg := ebiten.NewImage(width, int(barHeight))
	fg.Fill(color.RGBA{255, 0, 0, 255})
	opt2 := &ebiten.DrawImageOptions{}
	opt2.GeoM.Translate(x, y)
	screen.DrawImage(fg, opt2)
}
